// run: $exec < input
#include <cstdio>
#include <vector>

void scan(int & a)
{
	a = 0;
	bool flag = false;
	char ch = std::getchar();
	if (ch == '-')
		flag = true;
	else if (ch >= '0' && ch <= '9')
		a = ch - '0';

	while ((ch = std::getchar()) >= '0' && ch <= '9')
		a = a * 10 + ch - '0';
	if (flag) a = -a;
}

int const maxn = 10007;
int a[maxn];
int n, ans;

std::vector<int> tree[maxn];

void add_edge(int x, int y)
{
	tree[x].push_back(y);
	tree[y].push_back(x);
}

int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }

void dfs(int u, int fa, int start, int dis)
{
	if (gcd(a[u], a[start]) == 1) ans += dis;
	for (int i = 0; i < (int)tree[u].size(); i++) {
		int v = tree[u][i];
		if (v == fa) continue;
		dfs(v, u, start, dis + 1);
	}
}

int main()
{
	scan(n);
	for (int i = 1; i <= n; i++) scan(a[i]);
	for (int i = 1, x, y; i < n; i++) {
		scan(x); scan(y);
		add_edge(x, y);
	}
	for (int i = 1; i <= n; i++) dfs(i, -1, i, 0);
	ans /= 2;
	printf("%d\n", ans);
}

